unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Btnx_Send: TButton;
    Btnx_1: TButton;
    procedure Btnx_SendClick(Sender: TObject);
  private
  public
  end;

  TDesComm = function (lpsend:pchar;lprecv:pChar;errmsg:pchar): Integer; cdecl;
  TDesCommEx = function (lpsend:array of pchar;lprecv:array of pChar;errmsg:array of pChar): Integer; stdcall;

  function DesComm(lpsend:pchar;lprecv:pChar;errmsg:pchar):Integer;stdcall;external 'GASDesComm.dll';
  //function DesCommR(lpsend:array of Char;lprecv:array of Char;errmsg:array of Char):Integer;cdecl;external 'GASDesComm.dll';

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils;


{$R *.dfm}

procedure TForm1.Btnx_SendClick(Sender: TObject);
var
  NumbA:Integer;
  Handl:THandle;
  FuncA:TDesComm;


  lpsend:string;
  lprecv:string;
  errmsg:string;

  plpsend:PChar;
  plprecv:PChar;
  perrmsg:PChar;

  {rlpsend:array of PChar;
  rlprecv:array of PChar;
  rerrmsg:array of PChar;}

  rlpsend:array [0..1023] of Char;
  rlprecv:array [0..4095]of Char;
  rerrmsg:array [0..254]of Char;


  Ah_Tran_Code   	  :string;
  Ah_Bill_Type    	:string;
  Ah_Bill_No      	:string;
  Ah_Sec_Exch_Acct	:string;
  Ah_Mas_Exch_Acct	:string;
  Ah_Amt          	:string;
  Ah_Sign_Date    	:string;
  Ah_Limit_Flag   	:string;
  Ah_Send_Date    	:string;
  Ah_Send_Time    	:string;
  Ah_Invalid_Date 	:string;
  Ah_Pay_Times    	:string;
  Ah_Audit_Opr_Id 	:string;
  Ah_Ret_Code     	:string;
  Ah_Reserve      	:string;

  PAh_Tran_Code   	:PChar;
  PAh_Bill_Type    	:PChar;
  PAh_Bill_No      	:PChar;
  PAh_Sec_Exch_Acct	:PChar;
  PAh_Mas_Exch_Acct	:PChar;
  PAh_Amt          	:PChar;
  PAh_Sign_Date    	:PChar;
  PAh_Limit_Flag   	:PChar;
  PAh_Send_Date    	:PChar;
  PAh_Send_Time    	:PChar;
  PAh_Invalid_Date 	:PChar;
  PAh_Pay_Times    	:PChar;
  PAh_Audit_Opr_Id 	:PChar;
  PAh_Ret_Code     	:PChar;
  PAh_Reserve      	:PChar;

  {Ah_Tran_Code   	  :string[4];
  Ah_Bill_Type    	:string[2];
  Ah_Bill_No      	:string[8];
  Ah_Sec_Exch_Acct	:string[20];
  Ah_Mas_Exch_Acct	:string[20];
  Ah_Amt          	:string[15];
  Ah_Sign_Date    	:string[8];
  Ah_Limit_Flag   	:string[1];
  Ah_Send_Date    	:string[8];
  Ah_Send_Time    	:string[8];
  Ah_Invalid_Date 	:string[8];
  Ah_Pay_Times    	:string[2];
  Ah_Audit_Opr_Id 	:string[4];
  Ah_Ret_Code     	:string[2];
  Ah_Reserve      	:string[30];}

  {Ah_Tran_Code   	  :array [0..3] of Char;
  Ah_Bill_Type    	:array [0..1] of Char;
  Ah_Bill_No      	:array [0..7] of Char;
  Ah_Sec_Exch_Acct	:array [0..19] of Char;
  Ah_Mas_Exch_Acct	:array [0..19] of Char;
  Ah_Amt          	:array [0..15] of Char;
  Ah_Sign_Date    	:array [0..7] of Char;
  Ah_Limit_Flag   	:Char;
  Ah_Send_Date    	:array [0..7] of Char;
  Ah_Send_Time    	:array [0..7] of Char;
  Ah_Invalid_Date 	:array [0..7] of Char;
  Ah_Pay_Times    	:array [0..1] of Char;
  Ah_Audit_Opr_Id 	:array [0..3] of Char;
  Ah_Ret_Code     	:array [0..1] of Char;
  Ah_Reserve      	:array [0..29] of Char;}

  {Ah_Tran_Code   	:PChar;
  Ah_Bill_Type    	:PChar;
  Ah_Bill_No      	:PChar;
  Ah_Sec_Exch_Acct	:PChar;
  Ah_Mas_Exch_Acct	:PChar;
  Ah_Amt          	:PChar;
  Ah_Sign_Date    	:PChar;
  Ah_Limit_Flag   	:PChar;
  Ah_Send_Date    	:PChar;
  Ah_Send_Time    	:PChar;
  Ah_Invalid_Date 	:PChar;
  Ah_Pay_Times    	:PChar;
  Ah_Audit_Opr_Id 	:PChar;
  Ah_Ret_Code     	:PChar;
  Ah_Reserve      	:PChar; }

  arrlpsend:array[0..139] of char;


  TextA:string;
begin
  NumbA:=-1;
  
  Ah_Tran_Code:='2001';
  Ah_Bill_Type:='31';
  Ah_Bill_No  :='01495706';
  Ah_Sec_Exch_Acct:='70114151011570      ';
  Ah_Mas_Exch_Acct:='70014151010047      ';
  Ah_Amt      :='000000000112950';
  Ah_Sign_Date:='20121221';
  Ah_Limit_Flag:='0';
  Ah_Send_Date:='20121221';
  Ah_Send_Time:='16342600';
  Ah_Invalid_Date:='20121231';
  Ah_Pay_Times:='01';
  Ah_Audit_Opr_Id:='0000';
  Ah_Ret_Code:='00';
  Ah_Reserve :='';
  Ah_Reserve:=TKzUtils.TryFormatCode(30,0,' ',' ');

  
  //TKzUtils.WritLog(Ah_Reserve);

  lpsend:=Ah_Tran_Code+ah_bill_type+ah_bill_no+Ah_Sec_Exch_Acct+ah_mas_exch_acct+ah_amt+ah_sign_date+ah_limit_flag+ah_send_date+ah_send_time
         +ah_invalid_date+Ah_Pay_Times+ah_audit_opr_id+ah_ret_code+ah_reserve;


  {lprecv:=Ah_Tran_Code+ah_bill_type+ah_bill_no+Ah_Sec_Exch_Acct+ah_mas_exch_acct+ah_amt+ah_sign_date+ah_limit_flag+ah_send_date+ah_send_time
         +ah_invalid_date+Ah_Pay_Times+ah_audit_opr_id+ah_ret_code+ah_reserve;}

  perrmsg:=StrAlloc(255);
  //GetMem(perrmsg,255);
  StrCopy(perrmsg,'');

  plpsend:=StrAlloc(1024);
  //CopyMemory(@plpsend,@lpsend,Length(lpsend));
  //GetMem(plpsend,1024);
  StrCopy(plpsend,PChar(lpsend));
  //StrCopy(plpsend,@(lpsend));

  //GetMem(plprecv,4096);
  plprecv:=StrAlloc(4096);
  StrCopy(plprecv,'');
  //StrCopy(plprecv,@(lprecv));

  //SetLength(rc)

  //ShowMessage(Length(Rlpsend));

  //ShowMessage(plpsend);
  NumbA:=DesComm(plpsend,@plprecv,@perrmsg);
  Writeln(format('result.code:%D',[NumbA]));
  Writeln(format('result.text:%S',[plprecv]));

  {Handl:=LoadLibrary('GASDesComm.dll');
  try
    @FuncA:=GetProcAddress(Handl,'DesComm');
    if Assigned(@FuncA) then
    begin
      try
        if ChkBox_Pchar.Checked then
        begin
          NumbA:=FuncA(plpsend,plprecv,perrmsg);
        end else
        begin
          NumbA:=FuncA(PChar(lpsend),PChar(lprecv),PChar(errmsg));
        end;
      except
        on e:Exception do
        begin
          raise E.Create(E.Message);
        end;
      end;
      Writeln(format('%D',[NumbA]));
    end;
  finally
    //FreeLibrary(Handl);
    //StrDispose(plpsend);
    //StrDispose(plprecd);
    //StrDispose(perrmsg);
    //FreeMem(plpsend);
    //FreeMem(plprecd);
    //FreeMem(perrmsg);
  end;}
end;

end.
